home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre1.z / postgre1 / test / postfs / filetest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  1.9 KB  |  86 lines

  1. /*
  2.  * A file test
  3.  * write chunks of random lengths (constrained to be > 4 in postgres version)
  4.  * read chunks from random positions (constrainted to be == 5 in postgres
  5.  * version) and verify the contents with that originally written.
  6.  */
  7. #ifdef UNIX
  8. #define p_open open
  9. #define p_read read
  10. #define p_write write
  11. #define p_close close
  12. #define p_lseek lseek
  13. #else
  14. #include "tmp/libpq-fs.h"
  15. #endif
  16. #include  <sys/file.h>
  17. #include <stdio.h>
  18.  
  19. #define max(x,y) ((x) < (y) ? (y) : (x))
  20.  
  21. void main(argc,argv)
  22.      int argc;       
  23.      char *argv[];
  24. {
  25.     int fd;
  26.     int cnt, total = 0;
  27.     unsigned char buf[128*1024];
  28.     int lastwrite = 0;
  29.     int i;
  30.     int bad = 0;
  31.     int blockboundary = 0;
  32.  
  33.     PQsetdb(getenv("USER"));
  34.     fd = p_open("a",O_RDWR|O_CREAT);
  35.     printf("%d fd\n",fd);
  36.     srandom(time(0));
  37.     if (fd < 0) {
  38.     printf ("bad open\n");
  39.     exit(1);
  40.     }
  41.     /* write some random bytes, write random lengths */
  42.     for (i = 0; i < sizeof(buf); i++) {
  43.     buf[i] = random()%sizeof(buf);
  44.     if ((random()%4) == 0 && (i-lastwrite+1) >= 1021) {
  45.         p_write(fd,&buf[lastwrite],i-lastwrite+1);
  46.         lastwrite = i+1;
  47.     }
  48.     }
  49.     if (i-lastwrite+1 > 0)
  50.       p_write(fd,&buf[lastwrite],max(i-lastwrite+1,10));
  51. #if 0
  52.     p_write(fd,&buf[0],sizeof(buf));
  53. #endif
  54.     /* read some random bytes */
  55.     for (i = 0; i < 500; i++) {
  56.     int loc, n;
  57.     unsigned char tst[5];
  58.     loc = random()%sizeof(buf);
  59.     p_lseek(fd,loc,SEEK_SET);
  60.     if ((loc/1024) != ((loc+5)/1024)) {
  61.         blockboundary++;
  62.     }
  63.     if ((n = p_read(fd,&tst[0],5)) != 5)
  64.       printf ("only read %d\n",n);
  65.     {
  66.         int j, match = 1;
  67.         for (j = 0; j < 5; j++) {
  68.         if (tst[j] != buf[loc+j]) {
  69.             match=0;
  70.             break;
  71.         }
  72.         }
  73.         if (!match) {
  74.         printf ("at loc %d, wanted %d, saw %d\n",loc+j,buf[loc+j],
  75.             tst[j]);
  76.         bad++;
  77.         }
  78.     }
  79.     }
  80.     printf ("%d of 500 bad should be 0.\n",bad);
  81.     printf ("%d block boundaries should be > 0 for a good test\n",
  82.         blockboundary);
  83.     p_close(fd);
  84.     PQfinish();
  85. }
  86.